﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>API</title>
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bootstrap/css/pub.css" rel="stylesheet" />
    <script src="../bootstrap/js/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/bootstrap-paginator.js"></script>
    <!--[if lt IE 9]>
        <script src="bootstrap/js/html5shiv.min.js"></script>
        <script src="bootstrap/js/respond.min.js"></script>
    <![endif]-->
    <script src="../config.js"></script>
    <script src="../bootstrap/js/Common.js"></script>
    <script src="../bootstrap/js/controls.js"></script>
    <script type="text/javascript">
        var vId = GetUrlParam("dataPointID");
        $(function () {
            $.ajax({
                url: host_name + "terminal/list",
                type: "post",
                success: function (data) {
                    var html = '';
                    $.each(data,
                        function (i) {
                            html += '<option value="' +
                                data[i].terminalID + '">' +
                                data[i].name + '</option>';
                        });
                    $("#terminalList").html(html);
                    $("#terminalList option:first").attr('selected', 'selected');
                    init(data[0].terminalID, 1);
                }
            });
        });
        //初始化
        function init(select, pgindex) {
            var menus = [
                //{ name: "返回变量列表", action: "openPreWin()", css: "btn-link" }
            ];
            $(".menu-container").menu({ items: menus });
            //列表
            var columns = [
                { name: "接入点名称", field: "accessPointID" }, { name: "终端编号", field: "terminalId" },  { name: "接入点路径", field: "fullPath" },
                {
                    name: "操作", field: "accessPointID", title: '操作',
                    formatter: function (value) {
                        return '<a href="javascript:void(0);" onclick="Associateddevice(\'' + value + '\')" class="btn btn-sm btn-info" ><i class="fa fa-trash-o"></i>关联</a>';
                    }
                }
            ];
            //选中的设备ID参数
            var param = { terminalId: select, pagesize: 10, pageindex: pgindex };
            $("#tab").datagrid({ columns: columns, dblclick: void (0) }).loadData(host_name + "terminalaccesspoint/listofchild", "post", param, function () {
                //获取页数
                $.ajax({
                    url: host_name + "terminalaccesspoint/count?terminalId=" + select,
                    type: "get",
                    success: function (data) {
                        if (data < 10) {
                            $("#example").hide();
                        }
                        else {
                            $("#example").show();
                            var totalcount = Math.ceil(data / 10)
                            //初始化分页
                            $('#pageLimit').bootstrapPaginator({
                                currentPage: pgindex,
                                totalPages: totalcount,//总页数
                                numberOfPages: pagenum,//显示页数
                                size: "normal",
                                bootstrapMajorVersion: 3,
                                alignment: "right",
                                itemTexts: function (type, page, current) {
                                    switch (type) {
                                        case "first": return "首页";
                                        case "prev": return "上一页";
                                        case "next": return "下一页";
                                        case "last": return "末页";
                                        case "page": return page;
                                    }//默认显示的是第一页。
                                },
                                onPageClicked: function (event, originalEvent, type, page) {//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
                                    init(select, page);
                                }
                            });
                        }
                    }
                });
            });
            $("#modal-save-btn").hide();
        }
        //将数据点关联至接入点
        function Associateddevice(val) {
            $.ajax({
                url: host_name + "accesspointmap/single",
                type: "get",
                data: { key: "DataPointID", val: vId },
                success: function (dp_ap) {
                    if (dp_ap != "" && dp_ap.accessPointID != val) {
                        if (confirm("当前数据点已关联至接入点[" + dp_ap.accessPointID + "],确认要修改吗？")) {
                            $.ajax({
                                url: host_name + "accesspointmap/single",
                                type: "get",
                                data: { key: "AccessPointID", val: val },
                                success: function (ap_dp) {
                                    if (ap_dp != "") {
                                        if (confirm("当前接入点已关联至数据点[" + ap_dp.dataPointID + "],确认要再关联吗？")) {
                                            $.ajax({
                                                url: host_name + "accesspointmap/add",
                                                type: "post",
                                                data: JSON.stringify({ datapointID: vId, terminalid: $("#terminalList").val(), accesspointid: val }),
                                                success: function (adata) {
                                                    alert("关联成功！");
                                                    //setTimeout(" $('.modal').modal('hide');", 1000);
                                                },
                                                error: function () {
                                                    alert("网络异常！");
                                                }
                                            });
                                        }
                                    }
                                    else {
                                        $.ajax({
                                            url: host_name + "accesspointmap/update",
                                            type: "post",
                                            data: JSON.stringify({ id: dp_ap.id, datapointID: vId, terminalid: $("#terminalList").val(), accesspointid: val }),
                                            success: function (edata) {
                                                alert("关联成功！");
                                                $("#tab").datagrid().refresh();
                                            },
                                            error: function () {
                                                alert("网络异常！");
                                            }
                                        });
                                    }
                                },
                                error: function () {
                                    alert("网络异常！");
                                }
                            });
                        }
                    } else if (dp_ap == ""){
                        $.ajax({
                            url: host_name + "accesspointmap/single",
                            type: "get",
                            data: { key: "AccessPointID", val: val },
                            success: function (data) {
                                if (data != "") {
                                    if (confirm("当前接入点已关联至数据点[" + data.dataPointID + "],确认要再关联吗？")) {
                                        $.ajax({
                                            url: host_name + "accesspointmap/add",
                                            type: "post",
                                            data: JSON.stringify({ datapointID: vId, terminalid: $("#terminalList").val(), accesspointid: val }),
                                            success: function (adata) {
                                                alert("关联成功！");
                                                $("#tab").datagrid().refresh();
                                            },
                                            error: function () {
                                                alert("网络异常！");
                                            }
                                        });
                                    }
                                }
                                else {
                                    $.ajax({
                                        url: host_name + "accesspointmap/add",
                                        type: "post",
                                        data: JSON.stringify({ datapointID: vId, terminalid: $("#terminalList").val(), accesspointid: val }),
                                        success: function (adata) {
                                            alert("关联成功！");
                                            $("#tab").datagrid().refresh();
                                        },
                                        error: function () {
                                            alert("网络异常！");
                                        }
                                    });
                                }
                            },
                            error: function () {
                                alert("网络异常！");
                            }
                        });
                    }
                }
            });
        }
        //终端下拉菜单切换
        function Onterminalchange(obj) {
            if ($(obj).val() !== "") {
                init($(obj).val(), 1);
            }
        }
        //返回变量列表
        function openPreWin() {
            self.location = "variable.html";
        }
    </script>
</head>
<body>
    <div class="container-fluid">
        <div class="row" style="margin-bottom:10px">
            <div class="col-xs-3">
                <select class="form-control" id="terminalList" onchange="Onterminalchange(this)"></select>
            </div>
            <!--<div class="col-xs-3">
                <select class="form-control" id="equipmentList" onchange="Ondevicechange(this)"></select>
            </div>-->
            <div class="col-xs-6"></div>
        </div>
        <div class="row">
            <div class="col-md-12 table-responsive">
                <table id="tab" class="table table-bordered table-hover"></table>
            </div>
        </div>
        <div class="row" style="margin-right: 10px;">
            <div id="example" style="text-align: right">
                <ul id="pageLimit"></ul>
            </div>
        </div>
    </div>
</body>
</html>
